import { http } from "@/utils/http";
import { BindInfo, PageResponse } from "@/api/model/baseModel";
import { Permission } from "@/api/model/permissionModel";
import { Role } from "@/api/model/roleModel";

enum Api {
  PermissionList = "/permission/list",
  PermissionAll = "/permission/all",
  SavePermission = "/permission/save",
}

export function getPermissionList(param: Permission) {
  return http.get<PageResponse<Permission>>({
    url: Api.PermissionList,
    params: param,
  });
}

export function getPermissionAll() {
  return http.get<Role[]>({
    url: Api.PermissionAll,
  });
}

export function checkCode(code: string, id?: string) {
  return http.get<Boolean>({
    url: `/permission/check/${code}`,
    params: { id },
  });
}

export function changeState(id: string, state: boolean) {
  return http.post<Boolean>({
    url: `/permission/state/${id}/${state}`,
  });
}

export function savePermission(permission: Permission) {
  return http.post<Boolean>({
    url: Api.SavePermission,
    params: permission,
  });
}

export function permissionBindInfo(roleId: string) {
  return http.get<BindInfo<Permission>>({
    url: `/permission/bindInfo/${roleId}`,
  });
}

export function bindPermission(roleId: string, permissionIds: string[]) {
  console.log(permissionIds);
  return http.post<Permission[]>({
    url: `/permission/bind/${roleId}`,
    params: { permissionIds },
  });
}

export function unbindPermission(roleId: string, permissionIds: string[]) {
  console.log(permissionIds);
  return http.post<Permission[]>({
    url: `/permission/unbind/${roleId}`,
    params: { permissionIds },
  });
}

export function deletePermission(id: string) {
  return http.post<string>({
    url: `/permission/deletePermission/${id}`,
  });
}
